/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.webrep.metadata.processors;

import cz.muni.fi.pb138.webrep.repositories.XsdRepository;
import cz.muni.fi.pb138.webrep.util.Pair;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.basex.core.BaseXException;
import org.basex.core.Context;
import org.basex.core.cmd.XQuery;

/**
 *
 * @author pinkdidi
 */
public class XsdMetadataProcessor {

    private String getQueryForElementInSchema(String element, String file) {
        return "for $x in distinct-values(doc('" + file + "')//*[local-name() = '" + element + "'\n"
                + "   and namespace-uri() = 'http://www.w3.org/2001/XMLSchema']/@name)\n"
                + " where exists($x)\n"
                + " order by $x\n"
                + " return concat(data($x), '/!@#%')\n";
    }

    /**
     * Allows access to XSD metadata database. Uses data obtained in the boolean process(String pathToFile) method
     * @param pathToFile file containing needed data
     * @param artifactName name of the file
     * @param artifactVersion version of the artifact
     * @return true if success
     * @throws IOException 
     */
    public boolean process(String pathToFile, String artifactName,
            String artifactVersion) throws IOException {
        String res;
        Context context = new Context();
        int pos;
        try {
            res = new XQuery(getQueryForElementInSchema("complexType", pathToFile)).execute(context);
            String complexTypes[] = {};
            if((pos = res.lastIndexOf("/!@#%")) >= 0) res = res.substring(0, pos);
            if (!res.equals("")) {
                complexTypes = res.split("/!@#% ");
            }

            res = new XQuery(getQueryForElementInSchema("simpleType", pathToFile)).execute(context);
            String simpleTypes[] = {};
            if((pos = res.lastIndexOf("/!@#%")) >= 0) res = res.substring(0, pos);
            if (!res.equals("")) {
                simpleTypes = res.split("/!@#% ");
            }

            res = new XQuery(getQueryForElementInSchema("element", pathToFile)).execute(context);
            String elements[] = {};
            if((pos = res.lastIndexOf("/!@#%")) >= 0) res = res.substring(0, pos);
            if (!res.equals("")) {
                elements = res.split("/!@#% ");
            }

            res = new XQuery(getQueryForElementInSchema("attribute", pathToFile)).execute(context);
            String attributes[] = {};
            if((pos = res.lastIndexOf("/!@#%")) >= 0) res = res.substring(0, pos);
            if (!res.equals("")) {
                attributes = res.split("/!@#% ");
            }

            new XsdRepository().addNewMetadata(artifactName, artifactVersion,
                    complexTypes, simpleTypes, elements, attributes);
        } catch (BaseXException e) {
            return false;
        }
        return true;
    }

    /**
     * Allows access to XSD metadata database. 
     * @param pathToFile file containing needed data
     * @return
     * @throws IOException 
     */
    public boolean process(String pathToFile) throws IOException {
        int position;

        position = pathToFile.lastIndexOf(File.separator);
        String versionNumber = pathToFile.substring(position + File.separator.length());
        String rest = pathToFile.substring(0, position);

        position = rest.lastIndexOf(File.separator);
        String artifactName = rest.substring(position + File.separator.length());

        return process(pathToFile, artifactName, versionNumber);
    }

    /**
     * Method server to find all versions of XSD by certain restrictions.
     * @param restrictions map of restrictions, where key is metadata type and value is searched value
     * @return versions of artifact
     * @throws BaseXException
     * @throws IOException 
     */
    public Map<String, Set<Integer>> getArtifactVersionsByMetadata(List<Pair<String, String>> restrictions)
            throws BaseXException, IOException {
        return new XsdRepository().getArtifactVersionsByMetadata(restrictions);
    }

    /**
     * Method deletes all versions of document.
     * @param artifactName name of the XSD document
     * @throws BaseXException
     * @throws IOException 
     */
    public void deleteMetadata(String artifactName) throws BaseXException, IOException {
        new XsdRepository().deleteMetadata(artifactName);
    }
}
